package dao.Supper.impl;

import dao.BaseDao;
import dao.Supper.SuperBigDao;
import pojo.BigClass;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 大分类实现
 */
public class SuperBigDaoImpl extends BaseDao implements SuperBigDao {
    /**
     * 查询整合
     * @return
     */
   public List<BigClass> sel(String sql,Object[] params){
       List<BigClass> list = new ArrayList<>();
       ResultSet rs = this.executeSelect(sql, params);
       try {
           while (rs.next()) {
               BigClass bigClass = new BigClass();
               bigClass.setBigId(rs.getLong("id"));
               bigClass.setBigName(rs.getString("bigName"));
               bigClass.setBigText(rs.getString("bigText"));
               list.add(bigClass);
           }
       } catch (SQLException e) {
           e.printStackTrace();
       }finally {
           this.closeAll();
       }
       return list;
    }
    /**
     * 分页查询数据
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    public List<BigClass> selSbig(int currentPage, int pageSize) {
        String sql = "select * from bigclass limit ?,?";
        Object[] params = {(currentPage - 1) * pageSize, pageSize};
        return sel(sql,params);
    }

    /**
     * 查询全部---此处用于小分类渲染大分类名称
     * @return
     */
    public List<BigClass> selSbig(){
        String sql = "select * from bigclass";
        return sel(sql,null);
    }
    /**
     * 得到总记录条数
     *
     * @return
     */
    public int getTotalCount() {
        int count = 0;
        String sql = "SELECT COUNT(*) FROM bigclass";
        ResultSet rs = this.executeSelect(sql, null);
        try {
            if (rs != null) {
                while (rs.next()) {
                    count = rs.getInt(1);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll();
        }
        return count;
    }

    /**
     * 根据id进行查询
     *
     * @param id
     * @return
     */
    public List<BigClass> selSbjgById(Long id) {
        List<BigClass> list = new ArrayList<>();
        String sql = "select * from bigclass where id = ?";
        Object [] params = {id};
        return sel(sql,params);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delSbig(Long id) {
        int res = 0;
        try {
            String sql = "DELETE FROM bigclass WHERE id=? AND id NOT IN(SELECT smallBigId FROM smallclass)";
            Object[] params = {id};
            res = this.executeUpdateRows(sql,params);
        }finally {
            this.closeAll();
        }
        return res;
    }

    /**
     * 添加
     * @param bigClass
     * @return
     */
    public int addSbig(BigClass bigClass) {
        int res = 0;
        try {
            String sql = "INSERT INTO `bigclass`(`bigName`,`bigText`) VALUES (?,?)";
            Object[] params ={bigClass.getBigName(),bigClass.getBigText()};
            res = this.executeUpdateRows(sql,params);
        }finally {
            this.closeAll();
        }
        return res;
    }

    /**
     * 修改
     * @param bigClass
     * @return
     */
    public int updSbig(BigClass bigClass) {
        int res = 0;
        try{
            String sql="UPDATE `bigclass` SET `bigName`=?,`bigText`=? WHERE `id`=?";
            Object[] params = {bigClass.getBigName(),bigClass.getBigText(),bigClass.getBigId()};
            res = this.executeUpdateRows(sql,params);
        }finally {
            this.closeAll();
        }
        return res;
    }

    /**
     * 验证是否存在相同的昵称
     * @param name
     * @return
     */
    public int checkBigName(String name) {
        int count = 0;
        String sql = "SELECT COUNT(`bigName`) FROM `bigclass` WHERE `bigName` =?";
        Object[] params ={name};
        ResultSet rs = this.executeSelect(sql, params);
        try {
            if (rs != null) {
                while (rs.next()) {
                    count = rs.getInt(1);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll();
        }
        return count;
    }
}
